/**
 * 前端进行接口参数 加密  暂时不用
 */

import Md5 from 'crypto-js/md5';
import Base64 from 'crypto-js/enc-base64';
import UTF8 from 'crypto-js/enc-utf8';
import { parse as qsParse, stringify as qStringify } from 'qs';
import {currentUser} from '@/utils/commConstance';

const _typeof = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol' ? function(obj) {
  return typeof obj;
} : function(obj) {
  return obj && typeof Symbol === 'function' && obj.constructor === Symbol && obj !== Symbol.prototype ? 'symbol' : typeof obj;
};

/* eslint-disable */
const _extends = Object.assign || function(target) {
  for (let i = 1; i < arguments.length; i++) {
    const source = arguments[i];
    for (const key in source) {
      if (Object.prototype.hasOwnProperty.call(source, key)) {
        target[key] = source[key];
      }
    }
  }
  return target;
};

export const encrypt = {
  auth: function auth(request) {
    let params = null;
    const currentUserStringValue = window.sessionStorage.getItem(currentUser);
    if (!currentUserStringValue) {
      if (process.env.NODE_ENV === 'production') {
        // 返回到portal登录页面
        window.location.href = window.location.origin
      }
      return null;
    }
    const user = JSON.parse(currentUserStringValue);
    const token = user.token;
    const secret = user.secret;
    const timeStamp = Date.now();
    const once = Math.random().toString(16);
    switch (request.method) {
      case 'get':
      case 'delete':
        if (request.url.indexOf('?') > -1) {
          const parameters = request.url.split('?');
          params = encrypt.objKeySort(qsParse(parameters[1]));
        }
        if (request.params) {
          if (params === null) {
            params = encrypt.objKeySort(request.params);
          } else {
            params = params + encrypt.objKeySort(request.params);
          }
        }
        break;
      case 'post':
      case 'put':
        // 兼容图片上传的鉴权方式
        if (request.data instanceof FormData) {
          params = 'null';
        } else if (typeof request.data === 'string') {
          params = request.data;
        } else {
          params = JSON.stringify(request.data);
        }
        break;
      default:
        break;
    }
    let originSign = null;
    if (params === null) {
      originSign = token + timeStamp + once + secret;
    } else {
      originSign = token + timeStamp + once + params + secret;
    }
    const sign = Md5(originSign).toString().toUpperCase();
    return Base64.stringify(UTF8.parse(token)) + '.' + Base64.stringify(UTF8.parse(timeStamp)) + '.' + Base64.stringify(UTF8.parse(once)) + '.' + Base64.stringify(UTF8.parse(sign));
  },
  deepClone: function deepClone(obj) {
    if (Array.isArray(obj)) {
      return obj.reduce(function(result, val) {
        return [].concat(result, [encrypt.deepClone(val)]);
      }, []);
    }
    if (Object.prototype.toString.call(obj) === '[object Object]') {
      return Object.keys(obj).reduce(function(result, key) {
        let _extends2;
        const res = _extends({}, result, (_extends2 = {}, _extends2[key] = encrypt.deepClone(obj[key]), _extends2));
        return res;
      }, {});
    }
    return obj;
  },
  objKeySort: function objKeySort(params) {
    const newKeys = Object.keys(params).sort();
    let newParams = null;
    for (let i = 0; i < newKeys.length; i++) {
      if (params[newKeys[i]] === null || params[newKeys[i]] === '' || typeof params[newKeys[i]] === 'undefined' || _typeof(params[newKeys[i]]) === 'object' && params[newKeys[i]].length === 0) {
        continue;
      }
      if (newParams === null) {
        newParams = newKeys[i] + params[newKeys[i]];
        continue;
      }
      newParams += newKeys[i] + params[newKeys[i]];
    }
    return newParams;
  }
};
